//
//  MineSettingViewController.m
//  Find
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 Reiko. All rights reserved.
//

#import "MineSettingViewController.h"

@interface MineSettingViewController ()
@property (nonatomic, retain)UITextView  *feedbackTextView;
@end

@implementation MineSettingViewController
- (instancetype)init
{
    if (self = [super init]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)addNavItem
{
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Returns"] style:UIBarButtonItemStylePlain target:self action:@selector(actionBackButton:)];
    backButton.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backButton;
}
- (void)actionBackButton:(UIBarButtonItem *)backButton
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self addNavItem];
    
    // 设置标题
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 60)];
    
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"Helvetica" size:18];
    label.textColor = [UIColor whiteColor];
    
    // 初始化界面
    if ([self.flag isEqualToString:@"关于"]) {
        label.text = @"关于我的";
        [self addSubViewsAboutOus];
    } else {
         label.text = @"问题反馈";
        [self addSubViewFeedback];
    }
    self.navigationItem.titleView = label;
}

#pragma mark --  自定义界面
- (void)addSubViewsAboutOus
{
#pragma mark -- 左上角返回按钮
    //    lostBackButton.image = [UIImage imageNamed:@"lostDetailBack"];
    UIBarButtonItem * leftBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Returns"] style:(UIBarButtonItemStyleDone) target:self action:@selector(findDetailLeftBarButton)];
    leftBarButton.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
     NSString *context = @"1.关于开发这个App人员\n这个App由个人开发,版权归个人所有,并不能保证信息的绝度安全.\n2.关于怎么提出意见\n出门右转可以看到有一个""意见反馈""欢迎大家多多提出意见\n3.关于流量问题\n因为后台的服务器在云端,所以加载数据很费流量,建议在WiFi环境下使用.\n4.怎么联系开发人员\n提供一个邮箱:jyfzl@foxmail.com";
   
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    CGRect frame = [context boundingRectWithSize:CGSizeMake(kScreenWidth - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth - 20, frame.size.height)];
    label1.numberOfLines = 0;
    label1.text = context;
    label1.font = [UIFont fontWithName:@"Helvetica" size:16];
    scrollView.contentSize = CGSizeMake(kScreenWidth, frame.size.height + 20);
 
    [scrollView addSubview:label1];
    [self.view addSubview:scrollView];
    

}

#pragma mark--- 输入反馈意见
- (void)addSubViewFeedback
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth * 0.1,kScreenHeight * 0.05, kScreenWidth * 0.8, kScreenHeight * 0.05)];
    label.text = @"请输入反馈意见";
    label.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:label];
    label.textAlignment = NSTextAlignmentCenter;
    
   self.feedbackTextView = [[UITextView alloc] initWithFrame:CGRectMake(kScreenWidth * 0.1,label.bottom + 5, kScreenWidth * 0.8, kScreenHeight * 0.3)];
    self.feedbackTextView.font = [UIFont systemFontOfSize:17];
      self.feedbackTextView.layer.borderColor = [UIColor colorWithRed:87/255.0 green:191/255.0 blue:255/255.0 alpha:1].CGColor;
      self.feedbackTextView.layer.borderWidth =1.0;//该属性显示外边
      self.feedbackTextView.layer.cornerRadius = 6.0;//通过该值来设置textView边角的弧度
      self.feedbackTextView.layer.masksToBounds = YES;
      self.feedbackTextView.textColor = [UIColor blackColor];
      self.feedbackTextView.textAlignment = NSTextAlignmentLeft;
      self.feedbackTextView.autoresizingMask
    = UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.feedbackTextView];
    
#pragma mark -- 左上角返回按钮
    //    lostBackButton.image = [UIImage imageNamed:@"lostDetailBack"];
    UIBarButtonItem * leftBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Returns"] style:(UIBarButtonItemStyleDone) target:self action:@selector(findDetailLeftBarButton)];
    leftBarButton.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(kScreenWidth * 0.2, self.feedbackTextView.bottom, kScreenWidth * 0.6, kScreenHeight * 0.05);
    button.layer.cornerRadius = 6.0;//通过该值来设置textView边角的弧度
    button.layer.masksToBounds = YES;
    button.backgroundColor = [UIColor colorWithRed:0.341 green:0.749 blue:1.000 alpha:1.000];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(actionOkButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
}

// 左上角返回上一个界面
- (void)findDetailLeftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)actionOkButton:(UIButton *)button
{
    if ([self.feedbackTextView.text isEqualToString:@""]) {
        return;
    }
    AVObject *post = [AVObject objectWithClassName:@"Feedback"];
    AVUser *user = [AVUser currentUser];
    [post setObject:self.feedbackTextView.text forKey:@"suggestion"];
    [post setObject:user.objectId forKey:@"userID"];
    [post setObject:user.username forKey:@"userName"];
    [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [self showAlterViewWithTitle:@"提示" Message:@"发布成功"];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)showAlterViewWithTitle:(NSString *)title Message:(NSString *)message
{
    
   UIAlertController *alertView = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
   [self performSelector:@selector(closeAlter:) withObject:alertView afterDelay:1];
   [self presentViewController:alertView animated:YES completion:nil];
}
// 设置1秒之后消失
- (void)closeAlter:(UIAlertController *)alert
{
    [alert dismissViewControllerAnimated:YES completion:nil];
}


@end
